import React, { useEffect, useState } from 'react';
import { Select, Spin } from 'antd';
import { getDutylist } from '@/services/components/userform';
import { uniqBy } from 'lodash';

// 岗位下来选择框
const SelectDuty = ({ ...restProps }) => {
    const [data, setData] = useState([]);
    const [loading, setLoading] = useState(false);

    useEffect(() => {
        const fetchData = async () => {
            setLoading(true);
            try {
                const response = await getDutylist();
                if (response.code === 10000) {
                    const tempData = uniqBy(response.data || [], 'dutyCode'); // 去除重复数据，生成环境正常是不会有的，测试环境一堆重复数据，防止测试提bug
                    setData(tempData);
                }
            } finally {
                setLoading(false);
            }
        };
        fetchData();
    }, []);

    return (
        <Spin spinning={loading}>
            <Select
                mode="multiple"
                {...restProps}
                optionFilterProp="label"
                style={{ height: loading ? '35px' : 'auto' }}
            >
                {data.map(item => (
                    <Select.Option key={item.dutyCode} value={item.dutyCode} label={item.dutyAlias}>
                        {item.dutyAlias}
                    </Select.Option>
                ))}
            </Select>
        </Spin>
    );
};

export default SelectDuty;
